$(function() {
    class Forma {
        constructor() {
            this.emailrep = /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/
            this.userrep = /^[a-z0-9_-]{3,10}$/
            this.passwordrep = /^[a-zA-Z][a-z0-9_-]{6,13}$/
            this.coderep = /^[0-9]{6}$/
        }
        isEmail(value) {
            return this.emailrep.test(value)
        }
        isUser(value) {
            return this.userrep.test(value)
        }
        isPassword(value) {
            return this.passwordrep.test(value)
        }
        isCode(value) {
            return this.coderep.test(value)
        }
    }
    let forma = new Forma();
    $('#email').on('change', function() {
        let value = $(this).val()
            // alert(value)
        if (value == '' | value == null) {
            $('[data-toggle="popover1"]').popover('disable')
        }
        if (!forma.isEmail(value)) {
            $('[data-toggle="popover1"]').popover('enable').popover('show').attr('err', 1)
        } else {
            if ($('[data-toggle="popover1"]').attr('err')) {
                $('[data-toggle="popover1"]').removeAttr('err')
            }
            $('[data-toggle="popover1"]').popover('disable')
        }
    })
    $('#user').on('change', function() {
        let value = $(this).val()
        if (value == '' | value == null) {
            $('[data-toggle="popover2"]').popover('disable')
        }
        if (!forma.isUser(value)) {
            $('[data-toggle="popover2"]').popover('enable').popover('show').attr('err', 1)
        } else {
            if ($('[data-toggle="popover2"]').attr('err')) {
                $('[data-toggle="popover2"]').removeAttr('err')
            }
            $('[data-toggle="popover2"]').popover('disable')
        }
    })
    $('#password').on('change', function() {
        let value = $(this).val()
        if (value == '' | value == null) {
            $('[data-toggle="popover3"]').popover('disable')
        }
        if (value == '') {
            $('[data-toggle="popover3"]').popover('disable')
        }
        if (!forma.isPassword(value)) {
            $('[data-toggle="popover3"]').popover('enable').popover('show').attr('err', 1)
        } else {
            if ($('[data-toggle="popover3"]').attr('err')) {
                $('[data-toggle="popover3"]').removeAttr('err')
            }
            $('[data-toggle="popover3"]').popover('disable')
        }
    })
    $('#emailBox').on('change', function() {

        let value = $(this).val()
        if (value == '' | value == null) {
            $('[data-toggle="popover3"]').popover('disable')
        }
        if (!forma.isCode(value)) {
            $('[data-toggle="popover4"]').popover('enable').popover('show').attr('err', 1)
        } else {
            if ($('[data-toggle="popover4"]').attr('err')) {
                $('[data-toggle="popover4"]').removeAttr('err')
            }
            $('[data-toggle="popover4"]').popover('disable')
        }
    })

    //检测表单数据是否完成
    $('#resgiterbutton').on('click', function() {
        let isempty = false;
        let iserr = false
        $('.form-control').each(function() {
            if ($(this).val() == '') {
                isempty = true
                return
            }
            if ($(this).attr('err') == 1) {
                iserr = true
            }
        })
        if (isempty == true) {
            alert('您不能输入空表单！')
            return
        } else if (iserr == true) {
            alert('您的表单有误');
            return
        } else {
            $('#exampleModal').modal('show')
        }

    })

    //请求发起
    $('.btn-primary1').on('click', function() {
            // $('#exampleModal').slideUp()
            // $('body').removeClass('modal-open')
            $('#exampleModal').modal('toggle')
            let userinfo = {}
            $('.form-control').each(function() {
                let id = $(this).attr('id')
                userinfo[id] = $(this).val()
            })
            $.ajax({
                type: 'POST',
                url: 'http://localhost:5001/register',
                data: userinfo,
                success: function(result) {
                    if (result.msg == '注册成功') {
                        alert('恭喜您注册成功!')
                        location.href = 'login'
                    } else if (result.msg == '邮箱已经被注册') {
                        alert('邮箱已经被注册!')
                    } else {
                        alert('注册失败,请检查信息是否正确!')
                    }
                }

            })
        })
        //发邮件
    $('#code-box').on('click', function() {
        let email = $('#email').val()
        if (email == '' || email == null) {
            alert('请输入邮箱地址!')
            return
        }
        if (!forma.isEmail(email)) {
            alert('邮箱格式不正确')
            return
        }
        $.ajax({
            type: 'POST',
            data: {
                email,
            },
            url: 'http://localhost:5001/code',
            success: function(result) {
                console.log(result)
            }
        })
        $('.code-box').attr('disabled', true)
        let times = 6
        $(this).html(times + 's后重新发送')
        var interval = window.setInterval(() => {
            if (times == 0) {
                $('.code-box').html('获取邮箱验证码')
                window.clearInterval(interval)
                $('.code-box').attr('disabled', false)
                return
            }
            times--;
            $('.code-box').html(times + 's后重新发送')
        }, 1000)

    })
})